package com.gxljc.bear.crawler.util;

import com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import com.gxljc.commons.util.StringUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by junhhong on 2016/3/9.
 * <p/>
 * 将文本进行分词处理，计算Summary相似度
 */
public class TextUtil {


    /**
     * @param s1
     * @param s2
     * @return 根据两个Summary的相似度 进行打分
     */
    public static double getSimilarScore(String s1, String s2) {
        if (StringUtil.isNullOrEmpty(s1)
                || StringUtil.isNullOrEmpty(s2)) {
            return 0.0;
        }
        Set<String> set1 = getSegWordSetRemoveStopwords(s1);
        Set<String> set2 = getSegWordSetRemoveStopwords(s2);
        Set<String> allSet = new HashSet<String>(set1);
        Set<String> tempSet = new HashSet<String>(set1);
        allSet.addAll(set2);
        tempSet.retainAll(set2);
        return (double) tempSet.size() / (double) allSet.size();
    }

    /**
     * 分词 不包括标点和停用词
     *
     * @param subTitle
     * @return
     */
    public static Set<String> getSegWordSetRemoveStopwords(String subTitle) {
        Set<String> wordSet = new HashSet<String>();
        List<Term> terms = StandardTokenizer.segment(subTitle);
        for (Term term : terms) {
            if (shouldIncludeStopwords(term)) {
                wordSet.add(term.word);
            }
        }
        return wordSet;
    }

    private static boolean shouldIncludeStopwords(Term term) {
        if (term.nature == null)
            return false;
        String nature = term.nature.toString();
        char firstChar = nature.charAt(0);
        switch (firstChar) {
            case 'm': // 数词
            case 'b': // 区别词
            case 'c': // 连词
            case 'e': // 叹词
            case 'o': // 拟声词
            case 'p': // 介词
            case 'q': // 量词
            case 'u': // 助词
            case 'y': // 语气词
            case 'z': // 状态词
                // case 'r': //代词
            case 'w': // 标点
            {
                return false;
            }
            default: {
                if (term.word.length() > 1
                        && !CoreStopWordDictionary.contains(term.word)) {
                    return true;
                }
            }
            break;
        }

        return false;
    }

    public static boolean hasCommonRelation(String baiduR, String hudongR) {
        if (StringUtil.isNullOrEmpty(baiduR) || StringUtil.isNullOrEmpty(hudongR)) {
            return false;
        }
        String[] list1 = baiduR.split(StringUtil.STR_DELIMIT_3RD);
        Set<String> set1 = new HashSet<String>();
        for (String s : list1) {
            if (s.split(StringUtil.STR_DELIMIT_2ND).length < 2) {
                continue;
            }
            set1.add(s.split(StringUtil.STR_DELIMIT_2ND)[1]);
        }
        String[] list2 = hudongR.split(StringUtil.STR_DELIMIT_3RD);
        Set<String> set2 = new HashSet<String>();
        for (String s : list2) {
            if (s.split(StringUtil.STR_DELIMIT_2ND).length < 2) {
                continue;
            }
            set2.add(s.split(StringUtil.STR_DELIMIT_2ND)[1]);
        }
        set1.retainAll(set2);
        return set1.size() > 0;

    }
}
